#! /bin/sh
# PCP QA Test No. 254
# exercise pmNameAll for distributed PMNS with given namespace
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard filters
. ./common.product
. ./common.filter
. ./common.check

signal=$PCP_BINADM_DIR/pmsignal
_needclean=true
pmns="nameall.pmns"

mkdir $tmp
chmod ugo+rwx $tmp
cd $tmp

_cleanup()
{
    cd $here
    if $_needclean
    then
        _needclean=false
	$signal -s TERM $pmcd
	_wait_pmcd_end
	unset PMCD_SOCKET
	# address already in use seen on vm33, I'm unable to make it go away,
	# but is does not seem relevant to the success or otherwise of this
	# QA test, so filter it away - kenj Dec 2021
	#
	_filter_pcp_start <$tmp.err \
	| tee -a $seq_full \
	| sed \
	    -e '/__pmBind: Address already in use/d' \
	# end
	cat $tmp.log >>$seq_full
        echo "Restart and ping pmcd ..."
	# make sure pmcd's socket is torn down (failure on vm33, BSD 6.8)
	#
	sleep 2
	_service pmcd restart 2>&1 | _filter_pcp_restart
	_wait_for_pmcd
	_restore_auto_restart pmcd
	_service pmlogger restart 2>&1 | _filter_pcp_restart
	_wait_for_pmlogger
    fi
    rm -rf $tmp $tmp.*
}

_hunt_pmcd_socket()
{
    rm -f $tmp.tmp
    echo "hunting for pmcd socket ..." >>$seq_full
    netstat -a | grep ':44321' >>$seq_full
    if which fstat >/dev/null 2>&1
    then
	fstat | grep -E '[P]PID|:[4]4321' >$tmp.out 2>>$seq_full
	# fstat output ...
	# pcp      pmcd       81043    0* internet stream tcp 0x0 *:44321
	$PCP_AWK_PROG <$tmp.out '{ print $3 }' \
	| sed -e '/PID/d' \
	| LC_COLLATE=POSIX sort \
	| uniq >$tmp.tmp
    elif which fuser >/dev/null 2>&1
    then
	# fuser output is mixed on stderr and stdout!
	$sudo fuser 44321/tcp >$tmp.out 2>&1
	# fuser output ...
	# 44321/tcp:           624280
	$PCP_AWK_PROG <$tmp.out '{ print $2 }' \
	| LC_COLLATE=POSIX sort \
	| uniq >$tmp.tmp
    fi
    cat $tmp.out >>$seq_full
    if which pstree >/dev/null 2>&1
    then
	if pstree --help 2>&1 | grep '.--show-parents' >/dev/null
	then
	    _gnu_pstree=true
	else
	    _gnu_pstree=false
	fi
	cat $tmp.tmp \
	| while read pid
	do
	    echo "pid=$pid" >>$seq_full
	    if $_gnu_pstree
	    then
		pstree --show-parents --show-pids $pid >>$seq_full
	    else
		# BSD-style
		pstree -p $pid >>$seq_full
	    fi
	done
    fi
}

trap "_cleanup; exit \$status" 0 1 2 3 15

_stop_auto_restart pmcd

# real QA test starts here

cat >$pmns <<End-of-File
root {
    ten		29:0:11
    another_ten	29:0:11
    yet
}

yet {
    another
    again	29:0:11
}

yet.another {
    ten		29:0:11
}
End-of-File

_wait_pmcd_end || _exit 1
if ! _service pmlogger stop 2>&1; then _exit 1; fi \
| _filter_pcp_stop
_wait_pmlogger_end || _exit 1
if ! _service pmcd stop 2>&1; then _exit 1; fi \
| _filter_pcp_stop
_wait_pmcd_end || _exit 1
# make sure pmcd's socket is torn down (failure on vm33, BSD 6.8)
sleep 1

# Note: start pmcd with -f so that its PID stays the same (no daemon)
#
export PMCD_SOCKET=$tmp/pmcd.socket
_hunt_pmcd_socket
$PCP_PMCD_PROG -f -n $pmns -l $tmp.log 2>$tmp.err &
pmcd=$!
_wait_for_pmcd || _exit 1
_hunt_pmcd_socket

cd $here
src/nameall -s 2 ""

status=$?
